<?php
class LimitedAction extends CommonAction
{
    public function _initialize()
    {
        $this->assign('nav', 2);
        parent::_initialize();
    }

    public function index()
    {
        $this->display();
    }

    public function check()
    {
        if (isPost()) {
            $verify = strtoupper(getPost('verify'));
            if (!$verify) {
                $this->error('验证码不能为空');
            }
            if ($verify == '000000') {
                // 测试账号
                $user ['name'] = '测试账号';
                $userVerify ['phone'] = '133*******';
                $limited ['title'] = '测试发送';
                if (intval(getPost('isReceive'))) {
                    $this->assign('successMessage', '领取成功！');
                } else {
                    $this->assign('userVerify', $userVerify);
                    $this->assign('verify', $verify);
                    $this->assign('limited', $limited);
                    $this->assign('user', $user);
                }
                $this->display();
                exit ();
            }
            $LimitedUsersModel = D('LimitedUsers');
            // 判断输入验证码是否在领取库中
            $userVerify = $LimitedUsersModel->checkVerify($verify);
            if (!$userVerify) {
                $this->assign('errorMessage', '领取信息不存在！');
            } else {
                // 判断领取是否被用过
                if ($userVerify ['status'] == 2) {
                    $this->error('验证码信息已经被使用过！');
                }
                // 判断信息是否存在
                $uid = $userVerify ['uid'];
                $lid = $userVerify ['lid'];
                $user = D('Users')->find($uid);
                if (!$user) {
                    $this->error('用户信息不存在，不能进行领取！');
                }
                // 判断优惠信息是否存在
                $limited = D('Limited')->find($lid);
                if (!$limited) {
                    $this->error('优惠信息不存在，不能进行领取！');
                }
                // 对比领取时间 没有开始不能领取 领取时间过期不能领取
                $nowTime = time();
                if ($nowTime < $limited ['receive_start_time']) {
                    $this->error('领取还没有开始，不能进行领取！');
                }
                if ($nowTime > $limited ['receive_end_time']) {
                    $this->error('领取时间已经过，不能进行领取！');
                }
                // 判断已经领取的数量
                $receiveUsersCount = $LimitedUsersModel->getReceiveUsersCount($lid);
                if ($receiveUsersCount >= $limited ['count']) {
                    $this->error('数量领取完啦，不能进行领取！');
                }
                if (intval(getPost('isReceive'))) {
                    // 执行领取操作
                    $map = array();
                    $data = array();
                    $map ['verify'] = $verify;
                    $data ['receive_time'] = time();
                    $data ['receive_ip'] = get_client_ip();
                    $data ['status'] = 2;
                    $result = $LimitedUsersModel->update($map, $data);
                    if ($result) {
                        $this->assign('successMessage', '领取成功！');
                    } else {
                        $this->assign('errorMessage', '领取失败！');
                    }
                } else {
                    // 显示信息
                    $this->assign('userVerify', $userVerify);
                    $this->assign('verify', $verify);
                    $this->assign('limited', $limited);
                    $this->assign('user', $user);
                }
            }
        }
        $this->display();
    }

    public function checkcode()
    {
        if (isPost()) {
            $verify = strtoupper(getPost('verify'));
            if (!$verify) {
                $this->error('验证码不能为空');
            }
            if ($verify == '000000' || $verify == '111111') {
                // 测试账号
                $password ['type'] = ($verify == '000000') ? 1 : 2;
                $password ['code'] = $verify;
                if (intval(getPost('isReceive'))) {
                    $this->assign('successMessage', '使用成功！');
                } else {
                    $this->assign('password', $password);
                }
                $this->assign('verify', $verify);
                $this->display();
                exit ();
            }
            $LimitedCodeModel = D('LimitedCode');
            // 判断输入验证码是否在领取库中
            $password = $LimitedCodeModel->checkVerify($verify);
            if (!$password) {
                $this->assign('errorMessage', '优惠券不存在！');
            }
            if ($password ['status'] == 2) {
                $this->error('优惠卷已经被使用过！');
            }
            if (intval(getPost('isReceive'))) {
                $map = array();
                $data = array();
                $map ['code'] = $verify;
                $data ['user_time'] = time();
                $data ['status'] = 2;
                $data ['update_ip'] = get_client_ip();
                $result = $LimitedCodeModel->update($map, $data);
                if ($result) {
                    $this->assign('successMessage', '使用成功！');
                } else {
                    $this->assign('errorMessage', '使用失败！');
                }
            } else {
                $this->assign('password', $password);
            }
        }
        $this->assign('verify', $verify);
        $this->display();
    }

    public function showUsers()
    {
        import("@.ORG.Util.Page");
        $LimitedUsersModel = D('LimitedUsers');
        $Page = new Page ($LimitedUsersModel->count());
        $users = $LimitedUsersModel->limit($Page->firstRow . ',' . $Page->listRows)->order('lid DESC, create_time DESC')->select();
        $this->assign('page', $Page->show());
        $this->assign('users', $users);
        $this->display();
    }

    public function showCodes()
    {
        import("@.ORG.Util.Page");
        $LimitedCodeModel = D('LimitedCode');
        $Page = new Page ($LimitedCodeModel->count());
        $codes = $LimitedCodeModel->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('page', $Page->show());
        $this->assign('codes', $codes);
        $this->display();
    }
}

?>